const Controller = require('egg').Controller;
/**
 * Controller基类 
 * 用于封装返回值和自定义异常
 */
class BaseController extends Controller {

  Success() {
    if(arguments.length == 1){
      this.ctx.body = {
        data : arguments[0],
        errcode : 0,
      };
      this.ctx.status = 201;
     }else if(arguments.length == 2){
      this.ctx.body = {
        data : arguments[1],
        errcode : 0,
      };
      this.ctx.status = arguments[0];
     }
  }

  Error() {
    if(arguments.length == 1){
      this.ctx.body = {
        errcode : 9999,
        errmsg : arguments[0]
      };
    }else if(arguments.length == 2){
      this.ctx.body = {
        errcode : arguments[0],
        errmsg : arguments[1]
      };
    }
    throw new Error(this.ctx.body['errmsg']);
  }

}
module.exports = BaseController;